import { values } from 'core-js/fn/array'

/*
 * @Author: your name
 * @Date: 2020-10-21 10:33:01
 * @LastEditTime: 2020-11-12 16:42:10
 * @LastEditors: Please set LastEditors
 * @Description: In User Settings Edit
 * @FilePath: /crowd-funding/src/utils/utils.js
 */
export function pxTorem(px) {
	const rem = 37.5
	return `${px / rem / 2}rem`
}
export function formatComment(count = 1) {
	if (!count) return ''
	try {
		switch (true) {
			case count > 0 && count < 100:
				return ''
			case count >= 100 && count < 1000:
				return count
			case count >= 1000 && count < 10000:
				return '999+'
			case count >= 10000: {
				const num = (count / 10000).toString()
				const before = num.split('.')[0]
				const after = num.split('.')[1] ? num.split('.')[1].slice(0, 1) : 0
				return `${before}.${after}万`
			}
			default:
				return ''
		}
	} catch (error) {
		return ''
	}
}
export function concatUrl(url, param) {
	const keys = Object.keys(param)
	const values = Object.values(param)
	let returnUrl = `${url}?`
	keys.map((item, idx) => {
		const pr = idx !== keys.length - 1 ? '&' : ''
		returnUrl = `${returnUrl}${item}=${values[idx]}${pr}`
		return item
	})
	return returnUrl
}

/**
 * @description: 转换数字 >100万显示保留一位有效数字 如10,0000.1w，小于1百万 显示100,000.00
 * @param {String|Number} num
 * @param {Boolean} needTransform  是否将大于100万需要转换成W
 * @return {String}
 */
export function handleNum(num, needTransform = true) {
	if (typeof num === 'string' && typeof num === 'number') return '0'
	let value = num
	if (typeof num === 'string') {
		value = parseFloat(num)
	}
	const excepV = value / 1000000
	if (needTransform && excepV > 1) {
		const subV = handleNum((value / 10000).toFixed(1), false)
		return `${subV}W`
	}
	value = `${value}`
	const arr1 = value.split('.')
	let digit = ''
	if (arr1.length > 1) {
		digit = `.${arr1.pop()}`
	}
	let temp = arr1[0]
	let result = ''
	while (temp.length > 3) {
		const tempL = temp.length
		result += `,${temp.slice(tempL - 3, tempL)}`
		temp = temp.slice(0, tempL - 3)
	}
	return `${temp}${result}${digit}`
}

/**
 * @description: 判断是否是生产环境
 */
export function isProduction() {
	const { runEnv } = process.env
	if (runEnv === 'pro') {
		return true
	}
	return false
}

/**
 * @description: 判断是否使用mock数据
 */
export function isUseMockData() {
	const { runEnv } = process.env
	if (runEnv === 'mock') {
		return true
	}
	return false
}

export function FpsCtrl(fps, callback) {
	let delay = 1000 / fps
	let time = null
	let frame = -1
	let tref

	function loop(timestamp) {
		if (time === null) time = timestamp
		const seg = Math.floor((timestamp - time) / delay)
		if (seg > frame) {
			frame = seg
			callback({
				time: timestamp,
				frame,
			})
		}
		tref = requestAnimationFrame(loop)
	}

	this.isPlaying = false

	this.frameRate = function (newfps) {
		if (!arguments.length) return fps
		fps = newfps
		delay = 1000 / fps
		frame = -1
		time = null
	}

	this.start = function () {
		if (!this.isPlaying) {
			this.isPlaying = true
			tref = requestAnimationFrame(loop)
		}
	}

	this.pause = function () {
		if (this.isPlaying) {
			cancelAnimationFrame(tref)
			this.isPlaying = false
			time = null
			frame = -1
		}
	}
}
